#include <ClipApi.h>

//----------------------------------------------------------------------------//

LPSTR StrToken( LPSTR szText, WORD wOcurrence, BYTE bSeparator, LPWORD pwLen )
{
   WORD wStart = 0, wEnd = 0, wCounter = 0;

   if( ! bSeparator )
     bSeparator = ' ';

   do {
      wStart = wEnd;

      if( bSeparator != ' ' )
      {
         if( szText[ wStart ] == bSeparator )
            wStart++;
      }
      else
      {
         while( szText[ wStart ] && szText[ wStart ] == bSeparator )
            wStart++;
      }

      if( szText[ wStart ] && szText[ wStart ] != bSeparator )
      {
         wEnd = wStart + 1;

         while( szText[ wEnd ] && szText[ wEnd ] != bSeparator )
            wEnd++;
      }
      else
         wEnd = wStart;

   } while( wCounter++ < wOcurrence - 1 && szText[ wEnd ] );

   * pwLen = wEnd - wStart;

   if( wCounter < wOcurrence )
      * pwLen = 0;

   return szText + wStart;
}

//----------------------------------------------------------------------------//

CLIPPER STRTOKEN()  // ( cText, nOcurrence, cSepChar ) --> cToken
{
   WORD wLen;
   LPSTR szToken = StrToken( _parc( 1 ), _parni( 2 ),
                             IF( ISCHAR( 3 ) && ( PCOUNT() > 2 ),
                                 * _parc( 3 ), 0 ), &wLen );

   _retclen( szToken, wLen );
}

//----------------------------------------------------------------------------//

CLIPPER STRCHAR() // cText, nCharPos  --> cChar
{
   _retclen( _parc( 1 ) + _parni( 2 ) - 1, 1 );
}

//----------------------------------------------------------------------------//

CLIPPER STRCAPFIRS() // F( cString ) --> cString first upper + lowercase
{
   LPSTR pStr = _parc( 1 );
   LPSTR pTemp = pStr + 1;
   BYTE  bChar;

   if( * pStr > 'a' && * pStr < 'z' )
   {
      * pStr -= 'a';
      * pStr += 'A';
   }

   while( bChar = * pTemp )
   {
      if( bChar > 'A' && bChar < 'Z' )
         * pTemp = bChar - 'A';
      pTemp++;
   }
   _retc( pStr );
 }

//----------------------------------------------------------------------------//